/**
 * Visual Blocks Language
 *
 * Copyright 2012 Google Inc.
 * http://blockly.googlecode.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Generating Arduino for logic blocks.
 * @author gasolin@gmail.com  (Fred Lin)
 */
'use strict';

goog.provide('Blockly.Arduino.logic');

goog.require('Blockly.Arduino');

Blockly.Arduino.control_repeat = function() {
  var value_name = Blockly.Arduino.valueToCode(this, 'TIMES', Blockly.Arduino.ORDER_ATOMIC);
  var statements_name = Blockly.Arduino.statementToCode(this, 'SUBSTACK');
  var repeat='repeat_'+Math.round(Math.pow(Math.random(),3)*100000000);
   Blockly.Arduino.definitions_ [repeat]='int '+repeat+' =0;';
  // TODO: Assemble JavaScript into code variable.
  if(window.board_name=="MRC"){
    var code = 'for (;'+repeat+'<' + value_name + ';) {\nif(analogRead(A7)>500){\nbreak;\n}\n' + statements_name + '\n'+repeat+'++;\n}\n';
  }else{
    var code = 'for (;'+repeat+'<' + value_name + ';) {\n' + statements_name + '\n'+repeat+'++;\n}\n';
  }
  return code;
};

Blockly.Arduino.control_if = function() {
  // If condition.
  var value_name = Blockly.Arduino.valueToCode(this, 'CONDITION', Blockly.Arduino.ORDER_ATOMIC);
  var statements_ccc = Blockly.Arduino.statementToCode(this, 'SUBSTACK');
  if(value_name==null || value_name==""){
    value_name=false;
  }
  // TODO: Assemble JavaScript into code variable.
  var code = 'if ('+value_name+') {\n'+statements_ccc+'}\n';
  return code;
};



Blockly.Arduino.control_if_else = function() {
  // If condition.
  var value_name = Blockly.Arduino.valueToCode(this, 'CONDITION', Blockly.Arduino.ORDER_ATOMIC);
  var statements_if = Blockly.Arduino.statementToCode(this, 'SUBSTACK');
  var statements_else = Blockly.Arduino.statementToCode(this, 'SUBSTACK2');
  if(value_name==null || value_name==""){
    value_name=false;
  }
  // TODO: Assemble JavaScript into code variable.
  var code = 'if ('+value_name+') {\n'+statements_if+'}\nelse {\n'+statements_else+'}\n';
  return code;
};



Blockly.Arduino.control_repeat_until = function() {
  var value_name = Blockly.Arduino.valueToCode(this, 'CONDITION', Blockly.Arduino.ORDER_ATOMIC);
  var statements_name = Blockly.Arduino.statementToCode(this, 'SUBSTACK');
  if(value_name==null || value_name==""){
    value_name=false;
  }
  // TODO: Assemble JavaScript into code variable.
  if(window.board_name=="MRC"){
    var code = 'while (true) {\nif(analogRead(A7)>500){\nbreak;\n}'+ '\nif('+value_name+') {\n  break;\n  }\n'+statements_name+'\n}\n';
  }else{
    var code = 'while (true) {\n'+ '\nif('+value_name+') {\n  break;\n  }\n'+statements_name+'\n}\n';
  }

  return code;
};


Blockly.Arduino.control_jump_out_loop = function() {
    //跳出循环
  var code = 'break;\n';
  return code;
};
